﻿package mortal.game.view.task.drama.operations.npctalk
{
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.display.*;

    public class TaskDramaTalkMask extends GSprite
    {
        private var _data:TaskDramaTalkData;
        private var _masks:Array;
        private var _curIndex:int;
        private var _onEnd:Function;
        private var _lineNum:int;
        private var _text:GTextFiled;
        private var _isProcessing:Boolean = false;
        private var _reWidthed:Boolean = false;
        private var _frameTimer:FrameTimer;
        private var _testFrame:int = 0;

        public function TaskDramaTalkMask()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._masks = [];
            this._frameTimer = new FrameTimer(1);
            this._frameTimer.addListener(TimerType.ENTERFRAME, this.onEveryFrame);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._data = null;
            DisplayUtil.delNotUse(this._masks, 0);
            this._masks = null;
            this._curIndex = 0;
            this._lineNum = 0;
            this._text = null;
            this._isProcessing = false;
            this._reWidthed = false;
            if (this._frameTimer != null)
            {
                this._frameTimer.stop();
                this._frameTimer = null;
            }
            this._onEnd = null;
            return;
        }// end function

        public function finishRightNow() : void
        {
            var _loc_2:ScaleBitmap = null;
            var _loc_1:int = 0;
            while (_loc_1 < this._masks.length)
            {
                
                _loc_2 = this._masks[_loc_1] as ScaleBitmap;
                _loc_2.x = 0;
                _loc_1++;
            }
            this.endAndCallback();
            return;
        }// end function

        public function reInit(param1:TaskDramaTalkData, param2:GTextFiled, param3:Function) : void
        {
            if (this._isProcessing)
            {
                this.stop();
            }
            this._data = param1;
            this._lineNum = param2.numLines;
            this._text = param2;
            this._onEnd = param3;
            this._curIndex = 0;
            this.refleshAndStart();
            return;
        }// end function

        public function reWidth(param1:Number) : void
        {
            var _loc_3:ScaleBitmap = null;
            if (this._data == null)
            {
                return;
            }
            if (this._isProcessing)
            {
                this._reWidthed = true;
                this._isProcessing = false;
                this.endAndCallback();
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._masks.length)
            {
                
                _loc_3 = this._masks[_loc_2] as ScaleBitmap;
                _loc_3.width = param1;
                _loc_3.height = this._data.talkFontSize + this._data.talkFontLeading + 6;
                _loc_3.x = 0;
                _loc_2++;
            }
            return;
        }// end function

        public function stop() : void
        {
            this._isProcessing = false;
            this._frameTimer.stop();
            return;
        }// end function

        private function refleshAndStart() : void
        {
            this.initMasks();
            this._testFrame = 0;
            this._isProcessing = true;
            this._reWidthed = true;
            this._frameTimer.start();
            return;
        }// end function

        private function onEveryFrame(param1) : void
        {
            var _loc_4:String = this;
            var _loc_5:* = this._testFrame + 1;
            _loc_4._testFrame = _loc_5;
            if (this._reWidthed)
            {
                this._reWidthed = false;
                return;
            }
            var _loc_2:* = this._masks[this._curIndex] as ScaleBitmap;
            if (this._text.numLines <= this._curIndex)
            {
                this.endAndCallback();
                return;
            }
            var _loc_3:* = this._text.getLineMetrics(this._curIndex);
            _loc_2.x = _loc_2.x + this._data.speed;
            if (_loc_2.x > _loc_3.width + 8 - this._data.rowWidth)
            {
                _loc_2.x = _loc_3.width + 8 - this._data.rowWidth;
                var _loc_4:String = this;
                var _loc_5:* = this._curIndex + 1;
                _loc_4._curIndex = _loc_5;
                if (this._curIndex >= this._lineNum)
                {
                    this.endAndCallback();
                }
            }
            return;
        }// end function

        private function endAndCallback() : void
        {
            this.stop();
            if (this._onEnd != null)
            {
                this._onEnd.apply();
                this._onEnd = null;
            }
            return;
        }// end function

        private function initMasks() : void
        {
            var _loc_1:int = 0;
            var _loc_2:ScaleBitmap = null;
            _loc_1 = 0;
            while (_loc_1 < (this._lineNum + 1))
            {
                
                if (_loc_1 < this._masks.length)
                {
                    _loc_2 = this._masks[_loc_1] as ScaleBitmap;
                }
                else
                {
                    _loc_2 = GlobalClass.getScaleBitmap(BitmapDataConst.AlphaBMD);
                    this._masks.push(_loc_2);
                    this.addChild(_loc_2);
                }
                _loc_2.width = this._data.rowWidth;
                _loc_2.height = this._data.talkFontSize + this._data.talkFontLeading + 6;
                _loc_2.x = -this._data.rowWidth;
                _loc_2.y = _loc_1 * _loc_2.height;
                _loc_1++;
            }
            return;
        }// end function

        public function getLineNum() : int
        {
            return this._lineNum;
        }// end function

    }
}
